package tests.delete;

import static org.junit.Assert.*;

import java.net.UnknownHostException;

import model.Budget;
import model.BudgetFactory;
import model.Line;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

import cc.test.bridge.LineInterface;
import cc.test.bridge.SublineInterface;

public class SublineDeleteTest00
{
	Budget budget;
	LineInterface line;
	SublineInterface subline;
	BudgetFactory factory;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception
	{
	}

	@Before
	public void setUp() throws Exception
	{
	}

	@After
	public void tearDown() throws Exception
	{
		Mongo mongo = null;
  		try
  		{
  			mongo = new Mongo();
  		} catch (UnknownHostException e)
  		{
  			e.printStackTrace();
  		} catch (MongoException e)
  		{
  			e.printStackTrace();
  		}
  		
          DB db = mongo.getDB("cc");
          DBCollection collection = db.getCollection(budget.getRepositoryName());
          collection.remove(new BasicDBObject("entry.id", budget.getId()));
	}

	@Test
	public void test()
	{
		factory = new BudgetFactory();
        
	 	 try
		 {
            //Create a budget
	        budget = (Budget) factory.create();
            budget.setName("Test budget");
            
            budget.setDescription("This is a delete test budget.");
            
            line = (Line) budget.createLine();
            
            subline = (Line) line.createSubline();
			line.add(subline);
            
			budget.add(line);
			
			subline.commit();
			line.commit();
            budget.commit();
            
            line.delete(subline);
            
            Boolean successful = budget.commit();
            
            if(!successful)
            	fail("Line failed to delete");
            
        }
        catch(Exception e) {
            fail("Creating Budget failed with exception: " + e);
        }
	}
}
